package service.impl;

import dao.AccountDao;
import dao.PetDao;
import dao.PetOwnerDao;
import dao.PetStoreDao;
import dao.impl.AccountDaoImpl;
import dao.impl.PetDaoImpl;
import dao.impl.PetOwnerDaoImpl;
import dao.impl.PetStoreDaoImpl;
import entity.Account;
import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import service.PetService;

import java.util.Date;
import java.util.List;

public class PetServiceImpl implements PetService {
    PetDao petDao = new PetDaoImpl();
    PetOwnerDao petOwnerDao = new PetOwnerDaoImpl();
    PetStoreDao petStoreDao = new PetStoreDaoImpl();
    AccountDao accountDao = new AccountDaoImpl();

    @Override
    public List<Pet> getPetList() throws Exception {
        List<Pet> petList = petDao.getPetList();
        return petList;
    }

    @Override
    public List<Pet> getStorePetList() throws Exception {
        List<Pet> storePetList = petDao.getStorePetList();

        return storePetList;
    }
    @Override
    public List<Pet> ownerPet(PetOwner petOwner1) throws Exception {
        return petDao.ownerPet(petOwner1);
    }

//商店卖,主人买
    @Override
    public boolean getBuy(Pet pet, PetOwner petOwner1) throws Exception {
        //主人余额
        int result = petOwner1.getMoney() - pet.getPrice();

        boolean flag = false;
        if (result < 0) {
            return flag;
        }
        int petmessage = ownerMassage(pet, petOwner1);  //调用修改主人信息方法

        if (petmessage > 0) {
            int i = storeMessage(pet);//调用修改商店信息方法

            if (i > 0) {
                int i1 = petMessage(pet, petOwner1);//调用修改宠物信息方法

                if (i1 > 0) {
                    int i2 = insertInAcc(pet, petOwner1);//添加记录

                    if (i2>0){
                        flag=true;
                    }
                }
            }
        }
        return flag;
    }


    //修改宠物表数据
    public int petMessage(Pet pet, PetOwner petOwner1) throws Exception {
        int upate = petDao.getBuy(pet.getId(), petOwner1.getId());
        return upate;
    }

    //修改主人信息
    public int ownerMassage(Pet pet, PetOwner petOwner1) throws Exception {
        int upate = petOwnerDao.getBuy(pet.getPrice(), petOwner1.getId());
        return upate;
    }

    //修改商店信息
    public int storeMessage(Pet pet) throws Exception {
        //System.out.println(pet.getStoreId()+"    "+ pet.getPrice());
        int upate = petStoreDao.getBuy(pet.getStoreId(), pet.getPrice());
        return upate;
    }

    //插入记录
    public int insertInAcc(Pet pet, PetOwner petOwner1) throws Exception {
        Account account = new Account();
        account.setDealType(1);//表示商店卖给主人
        account.setPetId(pet.getId());
        account.setSellerId(pet.getStoreId());
        account.setBuyerId(petOwner1.getId());
        account.setPrice(pet.getPrice());
        account.setDealTime(new Date());//交易时间
        int insert= accountDao.addAccount(account);
        return insert;
    }


//主人卖，商店买
    @Override
    public boolean sellPet(Pet pet, PetStore petStore, PetOwner petOwner1) throws Exception {
        //主人余额
        int result = petOwner1.getMoney() + pet.getPrice();

        boolean flag = false;
        if (result < 0) {
            return flag;
        }
        int petmessage = ownerUpdate(pet, petOwner1);  //调用修改主人信息方法
        if (petmessage > 0) {
            int i = storeUpdate(pet,petStore);//调用修改商店信息方法

            if (i > 0) {
                int i1 = petUpdate(pet, petOwner1);//调用修改宠物信息方法

                if (i1 > 0) {
                    int i2 = UpdateInAcc(pet, petOwner1);//添加记录

                    if (i2>0){
                        flag=true;
                    }
                }
            }
        }
        return flag;
    }
    //插入记录
    private int UpdateInAcc(Pet pet, PetOwner petOwner1) throws Exception {
        Account account = new Account();
        account.setDealType(2);//表示主人卖给商店
        account.setPetId(pet.getId());
        account.setSellerId(petOwner1.getId());
        account.setBuyerId(pet.getStoreId());
        account.setPrice(pet.getPrice());
        account.setDealTime(new Date());//交易时间
        int insert= accountDao.changeAccount(account);
        return insert;
    }
//修改宠物信息
    private int petUpdate(Pet pet, PetOwner petOwner1) throws Exception {
        int upate = petDao.changePet(pet.getId(), petOwner1.getId());
        return upate;
    }
//修改商店信息
    private int storeUpdate(Pet pet,PetStore petStore) throws Exception {

        int upate = petStoreDao.changeStore(petStore.getId(), pet.getPrice());
        return upate;
    }
//修改主人信息
    private int ownerUpdate(Pet pet, PetOwner petOwner1) throws Exception {
        int i = petOwnerDao.changeOwner(pet.getPrice(), petOwner1.getId());
        return i;
    }
}
